package com.twu.biblioteca;
import java.io.* ;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
//Job: To represent biblioteca
public class Library {


    private List<Book> books;
    private List<Movie> movies;
    private List<User> users;
    private User currentUser=null;

    public Library(List<Book> books, List<Movie> movies, List<User> users) {

        this.books = books;
        this.movies = movies;
        this.users = users;
    }
    public String welcomeMessage() {
        return "=*******************************=\n"+
                "= -***Welcome To Biblioteca***- =\n"+
                "=          BANGALORE            =\n"+
                "= ONLINE PUBLIC LIBRARY SYSTEM  =\n"+
                "=*******************************=";


    }
    public String displayMenu() {
        return "#=======****Menu****========#\n"+
                "#1) View all books          #\n"+
                "#2) Reserve a book          #\n"+
                "#3) Check membership details#\n"+
                "#4) View all movies#\n"+
                "#5) Exit                    #\n"+
                "Select an option: ";


    }

    public String selectMenuOption(int choice) {
        String displayMessage;
        switch (choice){
            case 1:
                viewAllBooks();
                displayMessage="Option 1 selected";
                break;
            case 2:
                displayMessage="Option 2 selected";
                login("santa","santa1234");
                viewAllBooks();
                System.out.println("\nWhich book would you like to reserve? ");
                int selectedBookNo = getNumberInput();
                System.out.println(reserveBook(selectedBookNo));
                break;
            case 3:
                displayMessage="Option 3 selected";
                if (userLoggedIn())
                    displayUser(currentUser);
                else
                    System.out.println("Please talk to a Librarian. Thank you");
                break;
            case  4:
                viewAllMovies();
                displayMessage="Option 4 selected";
                break;
            case  5:
                displayMessage="";
                break;
            default:
                displayMessage="Select a valid option!!" ;
                System.out.println(displayMessage);
        }

        return displayMessage;
    }

    private void displayUser(User currentUser) {
                System.out.println("\n\nName: "+currentUser.getName() + "\nPhone No: " +
                        currentUser.getPhoneNo() + "\nEmail Id: " + currentUser.getEmailId());
    }

    public String viewAllBooks(){
        int index = 1;
        String listOfBooks="LIST OF BOOKS\n";

        for (Book book : books) {
            listOfBooks+="\n"+(index + ") " + book.getTitle() + " by " + book.getAuthor());
            index++;
        }
        System.out.println(listOfBooks);
        return listOfBooks;
    }

    public String viewAllMovies(){
        int index = 1;
        String listOfMovies="  \t\tMovie\t\tYear\t\tDirector\t\tRating\n";
        for (Movie movie : movies) {
            listOfMovies+="\n"+(index + ")\t" + movie.getName() + "\t" + movie.getYear() +
                    "\t" + movie.getDirector() + "\t\t" + movie.getRating());
            index++;
        }
        System.out.println(listOfMovies);
        return listOfMovies;
    }
    public String reserveBook(Integer selectedBookNo){
        String reserveBookResult;
        Book book;

        if (selectedBookNo <= 0 || selectedBookNo > books.size())
            reserveBookResult="Book number not found.";
        else
        {
            book= books.get(selectedBookNo-1);
            if(book.isAvailable()){
                book.setAvailable(false);
                reserveBookResult="Thank You! Enjoy the book.";
            }
            else
                reserveBookResult="Sorry we don't have that book yet";
        }

        return reserveBookResult;
    }
    private int getNumberInput() {
        Scanner in = new Scanner(System.in);
        return in.nextInt();
    }
    public User login(String username, String password) {
        for (User user : users){
            if (user.getUsername().equals(username)){
                if(user.getPassword().equals(password))
                    currentUser=user;
            }
        }
        return currentUser;
    }

    private boolean userLoggedIn() {
        return !(currentUser == null);
    }
}
